﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Entity;

namespace DAL.Persistencia
{
    public class SubServicosFileDAL
    {

        private NewTierraEntities Con;

        public SubServicosFileDAL()
        {
            Con = new NewTierraEntities();
        }

        public void Salvar(SubServicosFile s)
        {
            try
            {
                Con.AddToSubServicosFile(s);
                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

        public List<SubServicosFile> ListarTodosRange(int IdRange, int IdQTransf)
        {
            try
            {
                if (IdRange != 0)
                    return Con.SubServicosFile.Where(s => s.Ranges_id == IdRange).ToList();
                else
                    return Con.SubServicosFile.Where(s => s.Quote_Transf_id == IdQTransf).ToList();

            }
            catch
            {
                throw;
            }
        }

        public List<SubServicosFile> ListarTodosQTrsf(int IdQTrans)
        {
            try
            {

                return Con.SubServicosFile.Where(s => s.Quote_Transf_id == IdQTrans).ToList();

            }
            catch
            {
                throw;
            }
        }

        public decimal SomaTotal(int IdRange, int IdQTrans)
        {
            try
            {
                if (IdRange != 0)
                    return Convert.ToDecimal(Con.SubServicosFile.Where(s => s.Ranges_id == IdRange).ToList().Select(c => c.Valor).Sum());
                else
                    return Convert.ToDecimal(Con.SubServicosFile.Where(s => s.Quote_Transf_id == IdQTrans).ToList().Select(c => c.Valor).Sum());
            }
            catch
            {
                throw;
            }
        }

        public SubServicosFile ObterPorId(int Id)
        {
            try
            {
                return Con.SubServicosFile.Where(s => s.SubServicosFile_id == Id).SingleOrDefault();
            }
            catch
            {
                throw;
            }
        }

        public void Atualiza(SubServicosFile novo)
        {
            try
            {
                SubServicosFile antigo = ObterPorId(novo.SubServicosFile_id);

                antigo.NomeFantasia = novo.NomeFantasia;
                antigo.NomeSupplier = novo.NomeSupplier;
                antigo.NomeSubItem = novo.NomeSubItem;
                antigo.ServDe = novo.ServDe;
                antigo.ServAte = novo.ServAte;
                antigo.Valor = novo.Valor;
                antigo.Moeda = novo.Moeda;
                antigo.Moeda_id = novo.Moeda_id;

                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

    }
}
